//
//  AppDelegate.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/19.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "AppDelegate.h"
#import "RootTabBarViewController.h"      //此类继承与tabbar类
#import "RootViewController.h"      //此类为自定义tabbar类
#import "LeftViewController.h"
#import "RightViewController.h"
#import "MMDrawerController.h"
#import "MMExampleDrawerVisualStateManager.h"

@interface AppDelegate ()<SinaWeiboDelegate,SinaWeiboRequestDelegate>


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    self.self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    /**
     * 使用继承与tabbar类的根控制器RootTabBarViewController
     */
//    RootTabBarViewController *rootTBVC = [[RootTabBarViewController alloc] init];
//    self.window.rootViewController = rootTBVC;
    
    
    /**
     * 使用自定的根控制器RootViewController
     */
//    RootViewController *rootVC = [[RootViewController alloc] init];
//    self.window.rootViewController = rootVC;
    
    
    /**
     * 添加左右滑动视图，所以根视图设为滑动视图主视图
     */
    LeftViewController *leftVC = [[LeftViewController alloc] init];
    RightViewController *rightVC = [[RightViewController alloc] init];
//    RootViewController *rootVC = [[RootViewController alloc] init];   //测试
    RootTabBarViewController *rootVC = [[RootTabBarViewController alloc] init];     //正式
    
    MMDrawerController *mmDV = [[MMDrawerController alloc] initWithCenterViewController:rootVC leftDrawerViewController:leftVC rightDrawerViewController:rightVC];
    
    
    /**
     * 设置侧视图的宽度
     */
    [mmDV setMaximumLeftDrawerWidth:125 animated:YES completion:nil];
    [mmDV setMaximumRightDrawerWidth:75 animated:YES completion:nil];
    
    
    /**
     * 设置手势区
     */
    [mmDV setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [mmDV setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    
    /**
     *  设置动画类型
     */
    [[MMExampleDrawerVisualStateManager sharedManager] setLeftDrawerAnimationType:MMDrawerAnimationTypeParallax];
    [[MMExampleDrawerVisualStateManager sharedManager] setRightDrawerAnimationType:MMDrawerAnimationTypeParallax];
    
    
    /**
     * 设置动画效果
     */
    [mmDV setDrawerVisualStateBlock:^(MMDrawerController *drawerController, MMDrawerSide drawerSide, CGFloat percentVisible) {
        
        MMDrawerControllerDrawerVisualStateBlock block;
        block = [[MMExampleDrawerVisualStateManager sharedManager]
                 drawerVisualStateBlockForDrawerSide:drawerSide];
        if(block){
            block(drawerController, drawerSide, percentVisible);
        }
    }];
    
    
    self.window.rootViewController = mmDV;
    
    
    

    
    
/*************************************************************************************************************/
    
    
    /**
     * 创建微博对象，在代理创建是位了保证，该对象不会被无意销毁，其他地方都通过delegate获得这个对象，这个对象时唯一的
     */
    self.sinaweibo = [[SinaWeibo alloc] initWithAppKey:kAPPKey appSecret:kAPPSecret appRedirectURI:kAPPRedirectURI andDelegate:self];
    
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *sinaweiboInfo = [defaults objectForKey:@"weiblogAuthData"];
    if ([sinaweiboInfo objectForKey:@"AccessTokenKey"] && [sinaweiboInfo objectForKey:@"ExpirationDateKey"] && [sinaweiboInfo objectForKey:@"UserIDKey"])
    {
        self.sinaweibo.accessToken = [sinaweiboInfo objectForKey:@"AccessTokenKey"];
        self.sinaweibo.expirationDate = [sinaweiboInfo objectForKey:@"ExpirationDateKey"];
        self.sinaweibo.userID = [sinaweiboInfo objectForKey:@"UserIDKey"];
    }
    

    return YES;
}

#pragma  -mark   SinaWeiboDelegate

- (void)sinaweiboDidLogIn:(SinaWeibo *)sinaweibo
{
    NSLog(@"sinaweiboDidLogIn userID = %@ accesstoken = %@ expirationDate = %@ refresh_token = %@", sinaweibo.userID, sinaweibo.accessToken, sinaweibo.expirationDate,sinaweibo.refreshToken);
    
    [self storeAuthData];
}

- (void)storeAuthData
{
    SinaWeibo *sinaweibo = [self sinaweibo];
    
    NSDictionary *authData = [NSDictionary dictionaryWithObjectsAndKeys:
                              sinaweibo.accessToken, @"AccessTokenKey",
                              sinaweibo.expirationDate, @"ExpirationDateKey",
                              sinaweibo.userID, @"UserIDKey",
                              sinaweibo.refreshToken, @"refresh_token", nil];
    [[NSUserDefaults standardUserDefaults] setObject:authData forKey:@"weiblogAuthData"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)sinaweiboDidLogOut:(SinaWeibo *)sinaweibo{
    kSinaWeiboLog(@"退出成功！");
}


- (void)sinaweiboLogInDidCancel:(SinaWeibo *)sinaweibo{
    
}
- (void)sinaweibo:(SinaWeibo *)sinaweibo logInDidFailWithError:(NSError *)error{
    kSinaWeiboLog(@"登录失败！");
}
- (void)sinaweibo:(SinaWeibo *)sinaweibo accessTokenInvalidOrExpired:(NSError *)error{
    
}





- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
